package com.ctcd.security.service;

import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.ctcd.security.mapper.UserRepository;
import com.ctcd.security.pojo.User;
import com.ctcd.security.utils.ExcelOpration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @version 1.0
 * @Author tw
 * @Date 2024/12/17 9:56
 * @注释
 */

@Service
public class UserBatchToDaoImp {


    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    //批量增加
    public void insertBatch(){

        MybatisBatch<User> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, ExcelOpration.updateDept(ExcelOpration.getAll(),ExcelOpration.shift()));
        MybatisBatch.Method<User> method = new MybatisBatch.Method<>(UserRepository.class);
        mybatisBatch.execute(method.insert());
    }

    //批量更新
    public void updateBatch(){
        MybatisBatch.Method<User> mapperMethod  = new MybatisBatch.Method<>(UserRepository.class);

        new MybatisBatch<>(sqlSessionFactory,  ExcelOpration.updateDept(ExcelOpration.getAll(),ExcelOpration.shift())).saveOrUpdate(
                mapperMethod.insert(), // 指定insert方法

                ((sqlSession, User) -> sqlSession.selectList(mapperMethod.get("selectById").getStatementId(), User.getId()).isEmpty()), // 判断条件
                mapperMethod.updateById()); // 指定update方法
    }

}
